# coding=utf-8
# @Author: back
# @Time: 2024/10/10 11:40
import sys

from PySide6.QtCore import QCoreApplication, QMetaObject, QSize, Qt, QPropertyAnimation, QEasingCurve, QRect
from PySide6.QtGui import QIcon, QFont, QGuiApplication
from PySide6.QtWidgets import (QApplication, QGridLayout, QHBoxLayout, QLabel,
                               QMainWindow, QSizePolicy, QSpacerItem, QStackedWidget,
                               QStatusBar, QToolButton, QVBoxLayout, QWidget, )
import icon.icons


class UiMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setup_ui(self)
        self.width1 = None
        self.resize(1080, 720)
        self.toggleMenu(self.leftmenu)
        # self.init_slot()

    def setup_ui(self, mainwin):
        mainwin.resize(1080, 720)
        self.mainwidget = QWidget(mainwin)
        self.mainwidget.resize(1080, 720)
        self.v_layout = QVBoxLayout(self.mainwidget)
        self.topmenu = QWidget(self.mainwidget)
        size_policy = QSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
        size_policy.setVerticalStretch(1)
        size_policy.setHorizontalStretch(0)
        size_policy.setHeightForWidth(self.topmenu.sizePolicy().hasHeightForWidth())
        self.topmenu.setSizePolicy(size_policy)
        self.topmenu.resize(1060, 35)
        self.topmenu.setStyleSheet(u"background-color: rgb(102, 193, 235);")

        self.gridlayout1 = QGridLayout(self.topmenu)
        self.gridlayout1.setContentsMargins(20, 0, 0, 0)

        user = QToolButton(self.topmenu)
        icon_user = QIcon()
        icon_user.addFile(u":/users/mainwin-user.png", QSize(), QIcon.Mode.Normal, QIcon.State.Off)
        user.setIcon(icon_user)
        user.setIconSize(QSize(25, 25))
        self.gridlayout1.addWidget(user, 0, 0, 1, 1)

        horizontal_space = QSpacerItem(15, 20, QSizePolicy.Policy.Maximum, QSizePolicy.Policy.Minimum)
        self.gridlayout1.addItem(horizontal_space, 0, 1, 1, 1)

        self.menutable = QWidget(self.topmenu)
        self.menutable.resize(120, 34)
        self.h_layout = QHBoxLayout(self.menutable)
        self.h_layout.setContentsMargins(0, 0, 0, 0)

        self.label1 = QLabel(self.menutable)
        self.label1.setText("后台管理中心")
        font = QFont()
        font.setPointSize(15)
        self.label1.setFont(font)
        self.h_layout.addWidget(self.label1)

        self.gridlayout1.addWidget(self.menutable, 0, 2, 1, 1, Qt.AlignmentFlag.AlignHCenter)

        self.reduce = QToolButton(self.topmenu)
        self.reduce.setStyleSheet(u"background-color: rgb(63, 183, 255);")
        icon_reduce = QIcon()
        icon_reduce.addFile(u":/menus/reduce.png", QSize(), QIcon.Mode.Normal, QIcon.State.Off)
        self.reduce.setIcon(icon_reduce)
        self.reduce.setIconSize(QSize(20, 20))
        self.gridlayout1.addWidget(self.reduce, 0, 3, 1, 1)

        self.expand = QToolButton(self.topmenu)
        self.expand.setStyleSheet(u"background-color: rgb(42, 92, 255);")
        icon_expand = QIcon()
        icon_expand.addFile(u":/menus/expand.png", QSize(), QIcon.Mode.Normal, QIcon.State.Off)
        self.expand.setIcon(icon_expand)
        self.expand.setIconSize(QSize(20, 20))
        self.gridlayout1.addWidget(self.expand, 0, 4, 1, 1)

        self.close1 = QToolButton(self.topmenu)
        self.close1.setStyleSheet(u"background-color: rgb(225, 95, 68);")
        icon_close = QIcon()
        icon_close.addFile(u":/menus/close.png", QSize(), QIcon.Mode.Normal, QIcon.State.Off)
        self.close1.setIcon(icon_close)
        self.close1.setIconSize(QSize(20, 20))
        self.gridlayout1.addWidget(self.close1, 0, 6, 1, 1)

        self.v_layout.addWidget(self.topmenu)

        self.main_body = QWidget(self.mainwidget)
        size_policy1 = QSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
        size_policy1.setHorizontalStretch(0)
        size_policy1.setVerticalStretch(14)
        size_policy1.setHeightForWidth(self.main_body.sizePolicy().hasHeightForWidth())
        self.main_body.setSizePolicy(size_policy1)
        self.v_layout.addWidget(self.main_body)

        self.gridlayout2 = QGridLayout(self.main_body)
        self.gridlayout2.setContentsMargins(0, 0, 0, 0)

        self.leftmenu = QWidget(self.main_body)
        # size_policy2 = QSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
        # size_policy2.setHorizontalStretch(0)
        # size_policy2.setVerticalStretch(0)
        # size_policy2.setHeightForWidth(self.leftmenu.sizePolicy().hasHeightForWidth())
        # leftmenu.setSizePolicy(size_policy2)
        self.leftmenu.setMinimumSize(QSize(160, 0))
        self.leftmenu.setMaximumSize(QSize(75, 16777215))
        self.width1 = self.leftmenu.width()
        self.leftmenu.setStyleSheet(u"QWidget{background-color: rgb(228, 228, 228);}\n"
                                    "QToolButton{border-top: 3px outset transparent;\n"
                                    "border-bottom: 7px outset transparent;\n"
                                    "border-right: 3px outset transparent;\n"
                                    "border-left: 3px outset transparent;\n"
                                    "min-width: 55px;min-height: 55px;\n"
                                    "background-color: rgb(228, 228, 228);\n"
                                    "}\n"
                                    "QToolButton:hover{\n"
                                    "background-color: rgb(205, 205, 205);\n"
                                    "}\n"
                                    "QToolButton:pressed,QToolButton:checked{\n"
                                    "border-left: 3px outset rgb(93, 95, 97);background-color: rgb(200, 200, 200);\n"
                                    "}\n"
                                    "QPushButton:default {\n"
                                    "border-color: navy;\n"
                                    "}")

        self.v_layout2 = QVBoxLayout(self.leftmenu)
        self.v_layout2.setSpacing(2)
        self.v_layout2.setContentsMargins(9, 0, 9, 0)

        self.menu_button = QToolButton(self.leftmenu)
        self.menu_button.setText("菜单栏")
        font_menu = QFont()
        font_menu.setPointSize(12)
        self.menu_button.setFont(font_menu)
        icon_menu = QIcon()
        icon_menu.addFile(u":/menus/menu.png", QSize(), QIcon.Mode.Normal, QIcon.State.Off)
        self.menu_button.setIcon(icon_menu)
        self.menu_button.setIconSize(QSize(48, 48))
        self.menu_button.setCheckable(True)
        self.menu_button.setAutoExclusive(True)
        self.menu_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)

        self.users_button = QToolButton(self.leftmenu)
        self.users_button.setText("用户管理")
        self.users_button.setFont(font_menu)
        icon_users = QIcon()
        icon_users.addFile(u":/users/user.png", QSize(), QIcon.Mode.Normal, QIcon.State.Off)
        self.users_button.setIcon(icon_users)
        self.users_button.setIconSize(QSize(48, 48))
        self.users_button.setCheckable(True)
        self.users_button.setAutoExclusive(True)
        self.users_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)

        self.order_button = QToolButton(self.leftmenu)
        self.order_button.setText("订单")
        self.order_button.setFont(font_menu)
        icon_order = QIcon()
        icon_order.addFile(u":/orders/order.png", QSize(), QIcon.Mode.Normal, QIcon.State.Off)
        self.order_button.setIcon(icon_order)
        self.order_button.setIconSize(QSize(48, 48))
        self.order_button.setCheckable(True)
        self.order_button.setAutoExclusive(True)
        self.order_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)

        self.device_button = QToolButton(self.leftmenu)
        self.device_button.setText("设备")
        self.device_button.setFont(font_menu)
        icon_device = QIcon()
        icon_device.addFile(u":/devices/device.png", QSize(), QIcon.Mode.Normal, QIcon.State.Off)
        self.device_button.setIcon(icon_device)
        self.device_button.setIconSize(QSize(48, 48))
        self.device_button.setCheckable(True)
        self.device_button.setAutoExclusive(True)
        self.device_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)

        vertical_spacer = QSpacerItem(20, 340, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Maximum)

        self.label_bottom = QLabel(self.leftmenu)
        self.label_bottom.setMaximumSize(QSize(200, 20))
        self.label_bottom.setText("798慧生活")

        self.v_layout2.addWidget(self.menu_button, 0, Qt.AlignmentFlag.AlignHCenter)
        self.v_layout2.addWidget(self.users_button, 0, Qt.AlignmentFlag.AlignHCenter)
        self.v_layout2.addWidget(self.order_button, 0, Qt.AlignmentFlag.AlignHCenter)
        self.v_layout2.addWidget(self.device_button, 0, Qt.AlignmentFlag.AlignHCenter)
        self.v_layout2.addItem(vertical_spacer)
        self.v_layout2.addWidget(self.label_bottom, 0, Qt.AlignmentFlag.AlignHCenter)

        self.gridlayout2.addWidget(self.leftmenu, 0, 0, 1, 1, Qt.AlignmentFlag.AlignHCenter)

        self.stack_widget = QStackedWidget(self.main_body)
        self.stack_widget.resize(980, 645)
        self.gridlayout2.addWidget(self.stack_widget, 0, 1, 1, 1)

        self.close1.clicked.connect(mainwin.close)
        self.reduce.clicked.connect(mainwin.showMinimized)

        self.stack_widget.setCurrentIndex(0)

        self.animation = QPropertyAnimation(self.leftmenu, b"minimumWidth")
        self.animation.setDuration(500)  # ms
        self.animation.setStartValue(QRect(0, 0, 75, 647))
        self.animation.setEndValue(QRect(0, 0, 160, 647))
        self.animation.setEasingCurve(QEasingCurve.Type.InOutQuint)
        self.animation.start()

    def center(self):
        screen = QGuiApplication.primaryScreen()
        center = screen.geometry().center()
        x_pos = center.x() - self.width() // 2
        y_pos = center.y() - self.height() // 2
        self.move(x_pos, y_pos)

    # def left1(self):
    #     self.leftmenu1 = self.leftmenu
    #     self.width2 = self.leftmenu1.width()

    def toggleMenu(self, enable):
        if enable:
            standard = 75
            maxExtend = 160
            width = self.leftmenu.width()
            print(width)

            if width == standard:
                width_extended = standard
                print(width_extended)
                self.leftmenu.setMaximumSize(QSize(160, 1600000))

            else:
                width_extended = maxExtend
                print(width_extended)
                self.leftmenu.setMaximumSize(QSize(75, 750000))

        # animation
            self.animation = QPropertyAnimation(self.leftmenu)
            self.animation.setDuration(500)  # ms
            # self.animation.setStartValue(width)
            self.animation.setStartValue(QRect(150, 30, 100, 100))
            # self.animation.setEndValue(width_extended)
            self.animation.setEndValue(QRect(150, 30, 200, 200))
            # self.animation.setEasingCurve(QEasingCurve.Type.InOutQuint)
            self.animation.start()

    # def toggle_menu(self, enable):
    #     if enable:
    #         standard = 115
    #         maxExtend = 200
    #         self.width2 = self.leftmenu.width()

    def init_slot(self):
        # 绑定信号和槽
        self.menu_button.clicked.connect(self.toggleMenu(self.leftmenu))


if __name__ == '__main__':
    app = QApplication()
    window = UiMainWindow()
    window.show()
    sys.exit(app.exec())
